package exhi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.entity.XMsg;
import com.stp.core.spring3.BaseSpringAction;

import exhi.entity.EntpWarrant;
import exhi.manager.EntpWarrantMng;

@Controller
@RequestMapping("/entp")
public class EntpWarrantAct extends BaseAct {
	
	private static final String PATH_TABLE = "/warrant-table";
	private static final String PATH_LIST = "/warrant-list";
	private static final String PATH_DETAIL = "/warrant-detail";
	private static final String PATH_SAVE = "/warrant-save";
	private static final String PATH_DELETE = "/warrant-delete";
	
	// 大型国际展表 
	@RequestMapping(PATH_TABLE)
	public void table(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("code", code);

	}
	
	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String infoList(XGridPage page,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {

		String sql = "from EntpWarrant info where 1=1";
		sql += sqlModel.getSQLChars("info.code", "=", capCode, false, "and");
		sql += sqlModel.getSQLChars("info.sqr", "like", capName, false,
				"like");
		sql += " order by info.createtime desc";
		InfoGridRow row = new EntpWarrantAct.InfoGridRow();
		XGrid grid = entpWarrantMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}
	
	// 明细数据
	@RequestMapping(PATH_DETAIL)
	public void infoDetail(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpWarrant") EntpWarrant entpWarrantInfo,
			ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpWarrantInfo);

	}
	
	// 数据保存
	@RequestMapping(PATH_SAVE)
	@ResponseBody
	public String saveInfo(
			HttpServletRequest request,
			@ModelAttribute("entpWarrant") EntpWarrant entpWarrantInfo,
			ModelMap model) throws IllegalStateException, IOException {
		entpWarrantInfo.setCreatetime(new Date());
		entpWarrantMng.save(entpWarrantInfo);
		XMsg msg = new XMsg();
		String entpId = entpWarrantInfo.getId();
		msg.setState(true);
		//return "redirect:" + PATH_TABLE.substring(1);
		return toJson(msg);
	}
	
	@RequestMapping(PATH_DELETE)
	@ResponseBody
	public String infoDelete(
			@RequestParam(value = "ids", required = true, defaultValue = "") String ids) {
		delete("t_entp_warrant", "wid", ids);	
		XMsg msg = new XMsg();
		msg.setState(true);
		return toJson(msg);
	}
	
	@ModelAttribute("entpWarrant")
	public EntpWarrant getEntpSexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpWarrant entpWarrant = entpWarrantMng.get(id);
		if (null == entpWarrant) {
			entpWarrant = new EntpWarrant();
		}
		return entpWarrant;
	}
	
	class InfoGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			if (obj instanceof EntpWarrant) {
				xrow = new XGridRow();
				EntpWarrant info = (EntpWarrant) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add(info.getSqr());
				t.add(info.getDw());
				t.add(info.getBsqr());
				t.add("<a href='warrant-detail.jspx?infoId=" + info.getId()
						+ "&code=" + info.getCode() + "'>【编辑】</a>");
				xrow.setData(t);
			}
			return xrow;
		}
	}

	
	@Autowired
	@Qualifier("entpWarrantMng")
	private EntpWarrantMng entpWarrantMng;
}
